let that = null;
import store from "../../utils/factory";
import {
    addOrder,
} from "../../utils/config";
Page({
    data: {
        remark: "",
        address: null,
        name: "",
        phone: "",
        id: null,
        purchase: 0,
        // during: 26,
        startTime: "",
        submitStatus: false,
        ysDetail: {},
        delivered: null,
        check: false,
        radioColor: "#EB6681",
    },
    onLoad: function (e) {
        that = this;
    },
    checkAgreement(e) {
        this.setData({
            check: !this.data.check
        })
    },
    goAgreement() {
        wx.navigateTo({
            url: "../agreement/agreement"
        })
    },
    onShow() {
        if (!store.get("ysDetail")) {
            wx.showModal({
                content: "未获取月嫂信息",
                confrimText: "我知道了",
                showCancel: false,
                success: function (res) {
                    wx.navigateBack({ //返回上一页面或多级页面
                        delta: 1
                    })
                }
            })
            return false
        }
        let ysDetail = store.get("ysDetail")
        this.setData({
            ysDetail: ysDetail,
            submitStatus: false
        })
        console.log(store.get("ysDetail"))
    },
    watchRemark(e) {
        this.setData({
            remark: e.detail.value
        });
    },
    radiochange: function (e) {
        this.setData({
            delivered: e.detail.value
        })
        console.log('radio发生change事件，携带的value值为：', e.detail.value)
    },
    watchName(e) {
        this.setData({
            name: e.detail.value
        })
    },
    watchPhone(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    watchAddress(e) {
        this.setData({
            address: e.detail.value
        })
    },
    // watchDuring(e) {
    //     this.setData({
    //         during: e.detail.value
    //     })
    // },
    bindDateChange: function (e) {
        console.log(e)
        this.setData({
            startTime: e.detail.value
        })
    },
    payWX(data) {
        wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.packageValue,
            signType: "MD5",
            paySign: data.paySign,
            success: function (res) {
                wx.redirectTo({
                    url: `../resultPage/resultPage`
                });
            },
            fail: function (res) {
                wx.showToast({
                    title: "付款失败",
                    icon: "none"
                })
            },
            complete: function (res) {
                that.setData({
                    submitStatus: false
                })
            }
        });
    },
    addOrder: function (e) {
        let tmplId = "n_AU4x76-wZa_rqWzKpZa37iMmv2x6vktlvPNbHR4_s";
        if (this.data.submitStatus) {
            return false
        }
        if (!this.data.name) {
            wx.showModal({
                title: "温馨提示",
                content: "请输入姓名",
                showCancel: false
            })
            return false
        }
        if (!this.data.phone) {
            wx.showModal({
                title: "温馨提示",
                content: "请输入手机号码",
                showCancel: false
            })
            return false
        }
        if (!this.data.delivered) {
            wx.showModal({
                title: "温馨提示",
                content: "请选择分娩情况",
                showCancel: false
            })
            return false
        }
        if (!this.data.address) {
            wx.showModal({
                title: "温馨提示",
                content: "请输入服务地址",
                showCancel: false
            })
            return false
        }
        if (!this.data.startTime) {
            wx.showModal({
                title: "温馨提示",
                content: "请输入服务时间",
                showCancel: false
            })
            return false
        }
        if (!this.data.check) {
            wx.showModal({
                title: "温馨提示",
                content: "为确保您的服务体验,请点击同意开始我们的服务",
                confirmText: "同意", //默认是“确定”
                cancelText: "不同意",
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            check: true
                        })
                    }
                }
            })
            return false
        }
        this.setData({
            submitStatus: true
        })
        wx.requestSubscribeMessage({
            tmplIds: [tmplId],
            success(res) {
                console.log(res)
                if (res[tmplId] == "accept") {
                    wx.showModal({
                        title: "温馨提示",
                        content: "是否确认提交订单",
                        confrimText: "确定",
                        cancelText: "考虑一下",
                        confirmColor: "#EB6681",
                        cancelColor: "#222222",
                        success: function (res) {
                            if (res.confirm) {
                                that.setData({
                                    submitStatus: false
                                })
                                addOrder({
                                    "address": that.data.address,
                                    "phone": that.data.phone,
                                    "id": that.data.ysDetail.id,
                                    "name": that.data.name,
                                    "purchase": that.data.ysDetail.deposit,
                                    "remark": that.data.remark,
                                    "delivered": that.data.delivered,
                                    "startTime": that.data.startTime
                                }).then(a => {
                                    that.payWX(a.data)
                                })
                                    .catch(err => {
                                        that.setData({
                                            submitStatus: false
                                        })
                                    })
                            } else {
                                that.setData({
                                    submitStatus: false
                                })
                            }
                        }
                    })
                } else {
                    that.setData({
                        submitStatus: false
                    })
                    wx.showToast({
                        icon: "none",
                        title: "下单失败"
                    })
                }
            }
        })
    }
});